import vue from '@vitejs/plugin-vue';
import path from 'path';
import { defineConfig, loadEnv } from 'vite';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    plugins: [
      vue(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true, // 支持内联 JavaScript
        },
      },
    },
    server: {
      host: '0.0.0.0',
      port: 8888,
      // 代理
      proxy: {
        [env.VITE_BASE_API]: {
          target: env.VITE_HOST,
          changeOrigin: true,
          disableHostCheck: true,
          rewrite: (path) =>
            env.VITE_ENV === 'development' ? path.replace(/^\/dev-api/, '') : path.replace(/^\/prod-api/, ''),
        },
      },
    },
  };
});
